package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.AddressBean;

import java.util.List;

/**
 * Created by admin on 2016/12/13.
 */
public class ChooseAddressAdapeter extends BaseAdapter {
    private Context context;
    private View.OnClickListener listener;
    private List<AddressBean> addrList;

    public ChooseAddressAdapeter(List<AddressBean> addrList, View.OnClickListener listener, Context context) {
        this.addrList = addrList;
        this.listener = listener;
        this.context = context;
    }

    @Override
    public int getCount() {
        return addrList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChooseAddressViewHolder viewHolder = new ChooseAddressViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.choose_address_item, null);
            viewHolder.rl_choose_true = (RelativeLayout) convertView.findViewById(R.id.rl_choose_true);
            viewHolder.iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            viewHolder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChooseAddressViewHolder) convertView.getTag();
        }

        if (addrList.get(position).isSelect() == true) {
            viewHolder.iv_choose.setImageResource(R.mipmap.ic_choose_li);
//            viewHolder.rl_choose_true.setBackgroundResource(R.color.main_background);
        } else {
            viewHolder.iv_choose.setImageResource(R.mipmap.ic_choose_nor);
//            viewHolder.rl_choose_true.setBackgroundResource(R.color.white);
        }

        String province = addrList.get(position).getProvince();
        String city = addrList.get(position).getCity();
        String district = addrList.get(position).getDistrict();
        String place = addrList.get(position).getPlace();
        String addr = province + city + district + place;

        viewHolder.tv_name.setText(addrList.get(position).getConsignee());
        viewHolder.tv_phone.setText(addrList.get(position).getTelephone());
        viewHolder.tv_address.setText(addr);

        viewHolder.rl_choose_true.setOnClickListener(listener);
        viewHolder.rl_choose_true.setTag(position);

        return convertView;
    }

    class ChooseAddressViewHolder {
        RelativeLayout rl_choose_true;
        ImageView iv_choose;
        TextView tv_name;
        TextView tv_phone;
        TextView tv_address;
    }
}
